//import { login, getUserInfo, saveUser } from '@/api/user'
// import { jsonInBlacklist } from '@/api/jwt'
import router from '@/router/index'
import { message } from '@/utils/element'
import { defineStore } from 'pinia'
import { ref, computed, watch } from 'vue'
import { useRouterStore } from './router'
import {setCookie,getCookie,removeCookie} from '@/utils/cookie.js'

export const useUserStore = defineStore('user', () => {

  const userInfo = ref(JSON.parse(getCookie('userInfo') || '{}'))
  const setUserInfo = (data)=>{
    setCookie('userInfo',JSON.stringify(data))
    userInfo.value = data
  }

  const token = ref(getCookie('token') || '')
  const setToken = (data)=>{
    setCookie('token',data)
    token.value = data
  }
 
  const defaultRouterObject = ref(JSON.parse(sessionStorage.getItem('defaultRouterObject') || '{}'))
  const setDefaultRouterObject = (obj)=>{
    sessionStorage.setItem('defaultRouterObject',JSON.stringify(obj))
    defaultRouterObject.value = obj
  }



  // /* 获取用户信息*/
  // const GetUserInfo = async() => {
  //   const res = await getUserInfo()
  //   if (res.code === 0) {
  //     setUserInfo(res.data)
  //   }
  //   return res
  // }

  /* 设置侧边栏模式*/
  const skinColor = ref('dark')
  const changeSideMode = async(data) => {
    skinColor.value = data
  }

  const mode = computed(() => skinColor.value)
  const sideMode = computed(() => {
    if (skinColor.value === 'dark') {
      return '#191a23'
    } else if (skinColor.value === 'light') {
      return '#fff'
    } else {
      return skinColor.value
    }
  })
  const baseColor = computed(() => {
    if (skinColor.value === 'dark') {
      return '#fff'
    } else if (skinColor.value === 'light') {
      return '#191a23'
    } else {
      return '#fff'
    }
  })
  const activeColor = computed(() => {
    if (skinColor.value === 'dark' || skinColor.value === 'light') {
      return '#4D70FF'
    }
    return '#4D70FF'
  })

  // watch(token, () => {
  //   window.localStorage.setItem('token', token.value)
  // })

  // zty 2023.05.15
  const refreshFn = ref('') // 刷个页面数据函数
  const setRefreshFn = (fn)=>{
    refreshFn.value = fn
  }
  // end--

  const historys = ref([])

  const setHistory = (arr)=>{
    historys.value = arr
    //historys.value = JSON.parse(sessionStorage.getItem('historys'))
  }
  const pushHistory = (item)=>{
    historys.value.push(item)
  }
  const delHistory = (index,num)=>{
    historys.value.splice(index, num)
  }
  const initHistorys = ()=>{
    const initDta = [
        {
          name: defaultRouterObject.value.defaultRouter,
          meta: {
            title: defaultRouterObject.value.defaultRouterName,
          },
          query: {},
          params: {},
        },
    ]
    setHistory(initDta)
  }

   /* 登出*/
   const LoginOut = async() => {
    removeCookie('token')
    removeCookie('userInfo')
    sessionStorage.removeItem('activeValue')
    router.push({ name: 'Login', replace: true })
    window.location.reload()
  }

  const cashPages = ref([])
  const addCashPage = (pageName)=>{
    const index = cashPages.value.findIndex(item=>item==pageName)
    if(index<0){ // 不存在时才添加
      cashPages.value.push(pageName)
    } 
  }
  const removeCashPage = (pageName)=>{
    const index = cashPages.value.findIndex(item=>item==pageName)
    cashPages.value.splice(index, 1)
  }
  const clearCashPage = ()=>{
    cashPages.value =[]
  }
  

  return {
    userInfo,
    setUserInfo,
    token,
    setToken,
    //GetUserInfo,
    //LoginIn,
    LoginOut,
    changeSideMode,
    mode,
    sideMode,
    baseColor,
    activeColor,
    refreshFn,
    setRefreshFn,
    defaultRouterObject,
    setDefaultRouterObject,
    historys,
    setHistory,
    pushHistory,
    delHistory,
    initHistorys,
    cashPages,
    addCashPage,
    removeCashPage,
    clearCashPage
  }
})
